import { enemyPlane } from "../component/enemyPlane";
import emitter from "../emitter";
import enemyPlaneUI, { enemyPlaneUI_slo } from "./enemyPlaneUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class enemyPlane_0 extends enemyPlane {
    initData() {
        this.initEventListener();
        if (this.isAttack) this.planeIsAttack();
    }

    planeIsAttack() {
        let self = this;
        this.schedule(() => {
            self.attack();
        }, this.attackTime);
    }

    initEventListener() {
        emitter.on("setPlayerPosi", this.setEndPosi.bind(this));
        emitter.on("putEnemyBullet", this.putBullet.bind(this));;
    }

    onDisable() {
        emitter.off("setPlayerPosi", this.setEndPosi.bind(this));
        emitter.off("putEnemyBullet", this.putBullet.bind(this));;
    }

    private endPosi;
    private startPosi;
    private minPosi_y = -((cc.winSize.height / 2) + 800);
    private minPosi_x = -(cc.winSize.width / 2);
    private maxPosi_x = cc.winSize.width / 2
    setEndPosi(data) {
        if (this.endPosi || !this.isMove) return
        this.startPosi = this.node.position;
        let nodePosi = this.node.parent.convertToNodeSpaceAR(data);
        this.endPosi = nodePosi;
        this.setNodeMove(this.endPosi, this.moveSpeed);
    }

    attack() {
        this.getBullet(this.bulletNum, 0, 0, this.nodeBullet);
    }

    update() {
        //超出屏幕重置位置
        if (this.isMove && (this.node.y < this.minPosi_y || this.node.x > this.maxPosi_x || this.node.x < this.minPosi_x)) {
            let size = cc.winSize;
            let fangXiang = Math.random();
            let randPosi = Math.random() * (size.width / 2) - 80;
            let posi = fangXiang > 0.5 ? randPosi : -randPosi;
            this.node.setPosition(cc.v2(posi, this.startPosi.y));
            emitter.emit("resetPlayerPosi", this.setEndPosi.bind(this));
        }
        if (this.isMove) this.mob_lio();
    }
}
